define(function(require,exports,module){
	var aw = require("ajaxwrapper");
	var Select = require("select");
	var Dialog =require("dialog");
	var checkInRoomGridComp = require("../component/checkInRoomGrid");
	var checkInPerDetailGrid = require("../component/checkInPerDetailGrid");
	var ciiDinnerCalendar = require("../component/ciiDinnerCalendar");
	module.exports = {
		subnav: {
			handleReturnBtnClick: function(widget){
				widget.toList();
			},
			handleSaveBtnClick: function(widget){
				//车辆落实
				var ciiData = widget.get("curData");
				var carData = widget.get("cargrid").getData();
				//var dinnerData = widget.get("ciidinnergrid").getData();
				for(var i=0;i<carData.length;i++){
					if(typeof carData[i].haveCar != "string" ){
						carData[i].haveCar = carData[i].haveCar.key;
					}
					if(carData[i].haveCar == "true"){
						if(carData[i].address == "" || carData[i].carDate == ""){
							Dialog.alert({
								content: "地址或日期不能为空"
							});
						return false;
						}
						
					}
					carData[i].version=0;
					carData[i].status = (carData[i].haveCar == "true" ? "Pending" : "NoRequiement");
				}
				
				var pkCheckInDinner = widget.pkCheckInDinner;
				
				var cParams = {
						pkCIImplement: ciiData.pkCIImplement,
						version: ciiData.version,
						checkInCars: carData,
						dinners: [],
						cRoomPersonalInfos: [],
						signUpRooms:[],
						checkInDinner: pkCheckInDinner
					};
				
				
				//会议准备
				var meetingData = widget.get("checkMeetingDetailGrid").getData();
				if(meetingData!=null){
					for(var i = 0;i<meetingData.length;i++){
						meetingData[i].status = "Pending" ;
					}
					cParams.checkInMeeting=meetingData;
				}
				
				if(widget.get("dataSource") == "GroupReserve"){
					var roomData = widget.get("checkInRoomGrid").getData();
					var roomMap = {};
					var travelContractMap = {};
					for(var i=0;i<roomData.length;i++){
						if(!roomData[i].roomRegister){
							Dialog.alert({
								content: "请分配房间"
							});
							return false;
						}
						var roomRegister = roomData[i].roomRegister.pkRoomRegister;
						cParams.cRoomPersonalInfos.push({
							pkRoomPersonalInfo: roomData[i].pkRoomPersonalInfo,
							descript:roomData[i].descript,
							version: roomData[i].version || 0,
							pkCIImplement: ciiData.pkCIImplement,
							roomRegister: roomRegister,
							signUpRegister: roomData[i].signUpRegister.pkSignUpRegister,
							checkIn:false
						});
						
						if(roomData[i].signUpRegister.travelContract){
							if(roomMap[roomData[i].roomRegister.pkRoomRegister]){
								roomMap[roomData[i].roomRegister.pkRoomRegister].push(roomData[i].signUpRegister.travelContract.pkTravelContract);
							}else{
								roomMap[roomData[i].roomRegister.pkRoomRegister] = [];
								roomMap[roomData[i].roomRegister.pkRoomRegister].push(roomData[i].signUpRegister.travelContract.pkTravelContract);
							}
							for(var key in roomMap){
								$.unique(roomMap[key]);
							}
							
							var pkTravelContract = roomData[i].signUpRegister.travelContract.pkTravelContract;
							travelContractMap[pkTravelContract] = {
									roomNum:roomData[i].signUpRegister.travelContract.productRule.roomNum
							};
						}
						
					}
					// 判断权证可落实的房间数量
					if(travelContractMap){
						for(var key in travelContractMap){
							var num = 0;
							for(var j in roomMap){
								if($.inArray(parseInt(key),roomMap[j])>=0){
									num++;
								}
							}
							
							if(travelContractMap[key].roomNum !=null && travelContractMap[key].roomNum < num){
								Dialog.alert({
									tip: "提示！",
									content: "VIP的统一权证只能最多选择"+ travelContractMap[key].roomNum +"个房间"
								});
								return false;
							}
						}
					}
				}else{
					var signUpRoomData = widget.get("roomTypeEditGrid").getData();
					for(var i=0; i < signUpRoomData.length; i++){
						if(signUpRoomData[i].roomType){
							signUpRoomData[i].roomType = signUpRoomData[i].roomType.pkRoomType;
						}
						if(typeof signUpRoomData[i].roomStatus != "string"){
							signUpRoomData[i].roomStatus = signUpRoomData[i].roomStatus.key;
						}
					}
					cParams.signUpRooms = signUpRoomData;
				}
				widget.service.save(aw.customParam(cParams),function(){
					widget.get("grid").refresh();
					widget.toList();
				});
			}
		},
		grid: {
			handleSetBtnClick: function(widget,data,flag){
				$(".container el-grid el-grid-2").addClass("hidden");
				var dataSource = data.dataSource.key;
				widget.set("dataSource", dataSource);
				widget.toCard();
				if(dataSource == "GroupReserve"){
					checkInRoomGridComp.initComponent(null, widget,data.groupReserve?data.groupReserve.roomRegisters:null);
					widget.set("dataSource", "GroupReserve");
					aw.ajax({
						  url:"api/checkinimplement/queryroompersonal",
						  data:{
							  "pkCIImplement":data.pkCIImplement,
							  fetchProperties:"signUpRegister.personalInfo.pkPersonalInfo," +
							  	"signUpRegister.exitGroup," +
							  	"signUpRegister.travelContract.productRule.roomNum," +
							  	"signUpRegister.travelContract.pkTravelContract," +
	      						"signUpRegister.personalInfo.name," +
	      						"signUpRegister.personalInfo.sex.value," +
	      						"signUpRegister.personalInfo.idNumber," +
	      						"signUpRegister.personalInfo.mobilePhone," +
	      						"signUpRegister.pkSignUpRegister," +
	      						"roomRegister.pkRoomRegister," +
	      						"roomRegister.room.*," +
	      						"roomRegister.room.orgBuilding.pkOrgBuilding," +
	      						"roomRegister.room.orgBuilding.name," +
	      						"pkRoomPersonalInfo,descript,version"
						  },
						  dataType:"json", 
						  success:function(ret){
							  var checkInRoomGrid = widget.get("checkInRoomGrid");
							  checkInRoomGrid.setData(ret);
							  
							  for(var i=0;i<ret.length;i++){
								  if(flag){
									  widget.get("subnav").hide(["save"]);
									  widget.get("ciiCarForm").setDisabled(true);
//									  widget.get("ciiDinnerForm").setDisabled(true);
									  widget.get("ciiBaseForm").setDisabled(true);
								  }
							  }
						  }
					  });
					widget.get("ciiBaseForm").setData({
						name: data.groupReserve.planItemGroup.allianceOrg ? 
								data.groupReserve.planItemGroup.allianceOrg.name : data.groupReserve.planItemGroup.servicePoint.name,
						destination: data.groupReserve.planItemGroup.planItem.destination.name,
						checkInDate: data.groupReserve.checkInDate,
						checkOutDate: data.groupReserve.checkOutDate,
						signUpNum: data.signUpNum,
						description:data.groupReserve.description
					});
				}else{
					widget.get("ciiBaseForm").setData({
						name: data.nonMemGroupReserve.nonMemberGroup.contactName,
						destination: data.nonMemGroupReserve.nonMemberGroup.servicePoint.name,
						checkInDate: data.nonMemGroupReserve.nonMemberGroup.checkInDate,
						checkOutDate: data.nonMemGroupReserve.nonMemberGroup.checkOutDate,
						signUpNum: data.nonMemGroupReserve.nonMemberGroup.perNumber,
						description:data.nonMemGroupReserve.nonMemberGroup.description
					});
					if(data.nonMemGroupReserve.nonMemberGroup.signUpType && 
							data.nonMemGroupReserve.nonMemberGroup.signUpType.key == "Experience"){
						widget.get("ciiBaseForm").setValue("signUpNum", data.signUpNum);
					}
				}
				$("#roomPersonalInfos").removeClass("hidden");
				if(flag){
					$(".actions").eq(6).addClass("hidden");
					widget.get("subnav").hide(["save"]);
				}else{
					$(".actions").eq(6).removeClass("hidden");
					//widget.get("checkMeetingDetailGrid").refresh();
				}
				widget.set("curData",data);
				
				if(data.status.key=="Confirmed"){
					var datilCarGrid = widget.get("datilCarGrid");
					var carDis =data.carDispatchBill;
					var params={};
					var checkCarData = data.checkInCar;
					var carDisData=[];
					for(var j =0; j<checkCarData.length;j++){
						if(checkCarData[j].haveCar){
							if(carDis!=null && carDis.length>0){
								for ( var i=0;i<carDis.length;i++) {
									params={
											carDate:carDis[i].dispatchDate,
											address:carDis[i].address,
											description:carDis[i].description,
											haveCar:checkCarData[j].haveCar,
											driverName:carDis[i].driver.personalInfo.name==null?carDis[i].driverName:carDis[i].driver.personalInfo.name,
											driverPhone:carDis[i].driver.personalInfo.phone==null?carDis[i].driverPhone:carDis[i].driver.personalInfo.phone,
											carType:carDis[i].car.carType==null? carDis[i].carModel:carDis[i].car.carType,
											carNumber:carDis[i].car.carNumber
									}
								}
							}
						}else{
							params={
									carDate:checkCarData[j].carDate,
									address:checkCarData[j].address,
									description:checkCarData[j].description,
									haveCar:checkCarData[j].haveCar,
							}
						}
						carDisData.push(params);
					}
					
					datilCarGrid.setData(carDisData);
				}else{
					var carGrid = widget.get("cargrid");
				carGrid.setData(data.checkInCar);
				}
				//
			  	widget.get("roomTypeEditGrid").setData(data.nonMemGroupReserve==null?{}:data.nonMemGroupReserve.signUpRooms);
			  	widget.get("checkMeetingDetailGrid").setData(data.checkInMeetings);
			  	$(".J-grid-columns-descript").eq(0).click();
			  	
			  	widget.pkCheckInDinner = null;
			  	this.pkCheckInDinner = null;
				ciiDinnerCalendar.initComponent(widget,data,flag);
			  	
			  	
			},
			handleDetailBtnClick: function(widget,data){
				this.handleSetBtnClick(widget,data);
			},
			handlePreBtnClick: function(widget,data){
				widget.service.prepare(data,function(){
					widget.get("grid").refresh();
				});
			}
		}
	};
})
